let p = new Promise((reslove, reject) => {
    console.log("promise"); //立即执行
    reslove(111);
})
p.then(data => {
    //微任务
    console.log(data);
})
setTimeout(() => { //宏任务
    console.log("settimeout")
}, 0)
console.log("out") //全局执行

// for (var i = 0; i < 5; i++) {
//     (function (j) {
//         setTimeout(() => {
//             console.log(j);
//         }, j * 1000)
//     })(i)
// }

function deepCopy(obj) {

    function isObject(val) {
        return (typeof val === 'object' || typeof val === 'function') && val
    }
    if (!isObject(obj)) throw new Error('typeError');

    let newObj = Array.isArray(obj) ? [] : {};

    Reflect.ownKeys(obj).forEach(key => {
        newObj[key] = isObject(obj[key]) ? deepCopy(obj[key]) : obj[key];
    })

    return newObj;
}

function mySetTimeInterval(fn, timeout) {
    let timer = null;
    function interval() {
        fn();
        timer = setTimeout(interval, timeout);
    }
    timer = setTimeout(interval, timeout);
    return timer;
}
// mySetTimeInterval(() => {
//     console.log(1);
// }, 2000);

function debounce(fn, timeout) {
    let timer = null;
    return function (...args) {
        clearTimeout(timer);
        timer = setTimeout(() => {
            fn.apply(this, args);
        }, timeout)
    }
}

//节流
function throttle(fn, timeout) {
    let prev;
    return function (...args) {
        let now = +new Date();
        if (prev || (now - prev) >= timeout) {
            prev = now;
            fn.apply(this, args);
        }
    }
}


function fakeNew(Constructor, ...args) {
    let obj = {};
    Object.setPrototypeOf(obj, Constructor.prototype);
    let res = Constructor.apply(obj, args);
    return typeof res === 'object' ? res : obj;
}

function myInstanceof(left, right) {
    let pro = Object.getPrototypeOf(left);
    let prototy = right.prototype;
    while (pro !== null) {
        if(pro === prototy) return true;
        pro = Object.getPrototypeOf(pro)
    }
    return false;
}